<?php

class Package extends Controller {
	
	/**
	 * Constructor
	 */
	function Package(){
		parent::Controller();
		$this->load->model('Package_model');
	}
	
	/**
	 * Add a new package to the database
	 */
	function add_pkg(){
		if ($this->auth->has_rights_to("add","pkg"))
		{
			$infos = array(
	    					'name' => $this->input->post('name'),
							'website' => $this->input->post('website'),
							'resp_usr' => $this->auth->get_user_id(),
							'licence' => $this->input->post('licence'),
							'category' => $this->input->post('category'),
							'short_desc' => $this->input->post('short_desc')
			);
	    					
			$result = $this->Package_model->new_package($infos);
			if($result === true){
				$data = $this->Package_model->data;
				$path = $this->config->item('download').$data['pkg_id'];
				if (!mkdir($path))
					echo "can't create path"; 
				$this->load->view('package/addOk',$infos);
			}
			else{
				$param['values'] = array();
				foreach($infos as $key => $value){
					if(isset($result[$key])){
						$param['values'][$key.'_val'] = '';
					}else{
						$param['values'][$key.'_val'] = $value;
					}
				}
				
				$param['errors'] = array();
				foreach($infos as $key => $value){
					$param['errors'][$key.'_err'] = '';
				}
				foreach($result as $key => $value){
					$param['errors'][$key.'_err'] = $value;
				}
				$param['categories'] = $this->Package_model->get_categories(); 
				$this->load->view('package/add', $param);
			}
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}
	
	/**
	 * Display the add package form
	 */
	function add(){
		if ($this->auth->has_rights_to("add","pkg"))
		{
			$param['values'] = array('name_val' => '', 'website_val' => '','licence_val' => '','category_val' => '','short_desc_val' => '');
			$param['errors'] = array('name_err' => '', 'website_err' => '','licence_err' => '','category_err' => '','short_desc_err' => '');
			$param['categories'] = $this->Package_model->get_categories();
			if (!is_array($param['categories']))
     			$this->load->view('system_error');
     		else
				$this->load->view('package/add', $param);
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}
	
	/**
	 * Display the information of the package $var
	 */
	function load($id){
		if (is_numeric($id) && $this->auth->has_rights_to("lookat","pkg",$id))
		{
			$this->Package_model->load_package_from_id($id);
			$data = $this->Package_model->data;
			if (!is_array($data))
     			$this->load->view('system_error');
     		else
				$this->load->view('package/show',$data);
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}
	
	/**
	 * Display the package list with pagination
	 */
	function pkglist($page=0){
		if ($this->auth->has_rights_to("list","pkg"))
		{
			$this->load->model('Package_model');
			$this->load->library('paginationajax');
			
			$match = array('status' => 'active');
			
			$config['base_url'] = site_url("package/pkglist");
			$config['total_rows'] = $this->Package_model->get_pkg_count($match);
			$config['per_page'] = '5';
			$config['js_function'] = 'update_pkglist';
			
			$data['pkgtitle'] = "All packages";
			$data['pkglist']=$this->Package_model->get_packages($page, $config['per_page'], $match);
			if (!is_array($data['pkglist']))
     			$this->load->view('system_error');
     		else {
				$this->paginationajax->initialize($config);
				$data['pagination']=$this->paginationajax->create_links();
				$this->load->view('package/list',$data);
     		}
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}
	
	/**
	 * Display the search package form
	 */
	function search(){
		if ($this->auth->has_rights_to("list","pkg"))
		{
			$this->load->view('package/search');
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}
	
	/**
	 * Display the results of the search form with pagination
	 * where $searchkey matchs $searchvalue.
	 */
	function pkgresults($searchkey, $searchvalue, $page=0 ){
		if ($this->auth->has_rights_to("list","pkg"))
		{
			$this->load->model('Package_model');
			$this->load->library('paginationajax');
			
			$config['base_url'] = site_url('package/pkgresults/'.$searchkey.'/'.$searchvalue.'/');
		
			
			$config['per_page'] = '5';
			$config['js_function'] = 'update_search';
			$config['uri_segment'] = '5';
		
			$match = array();
			$match['status'] = 'active';
			$match[$searchkey] = $searchvalue;
			
			$config['total_rows'] = $this->Package_model->get_pkg_count($match);
			
			$data['pkgtitle'] = 'Packages matching "'.$searchvalue.'"';
			$data['pkglist']=$this->Package_model->get_packages($page, $config['per_page'], $match);

			if (!is_array($data['pkglist']))
     			$this->load->view('system_error');
     		else {
				$this->paginationajax->initialize($config);
				$data['pagination']=$this->paginationajax->create_links();
				$this->load->view('package/list',$data);
     		}
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}
	
	/**
	 * Display the edit package view in order to edit the package $id
	 */
	function edit($id){
		
		if (is_numeric($id) && $this->auth->has_rights_to("edit","pkg", $id))
		{
			$this->Package_model->load_package_from_id($id);
			$data = $this->Package_model->data;
			if (!is_array($data))
     			$this->load->view('system_error');
     		else {
				$data['categories'] = $this->Package_model->get_categories(); 
				if (!is_array($data['categories']))
	     			$this->load->view('system_error');
	     		else
					$this->load->view('package/edit',$data);
     		}
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}
	
	/**
	 * Save the new information of the package into the database
	 */
	function edit_pkg($pkg_id){
		if (is_numeric($pkg_id) && $this->auth->has_rights_to("edit","pkg", $pkg_id))
		{
			$infos = array (
							'name' => $this->input->post('name'),
							'website' => $this->input->post('website'),
							'licence' => $this->input->post('licence'),
							'category' => $this->input->post('category'),
							'status' => $this->input->post('status'),
							'short_desc' => $this->input->post('short_desc')
			);
			
			if (!$this->Package_model->save_package($pkg_id,$infos))
     			$this->load->view('system_error');
     		else
     			$this->load->view('editOk');
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}
	
	/**
	 * Delete the package $pkg_id from the database
	 */
	function delete($pkg_id) {
		if (is_numeric($pkg_id) && $this->auth->has_rights_to("delete","pkg", $pkg_id))
		{
			if (!$this->Package_model->save_package($pkg_id, array('status' => 'deleted')))
     			$this->load->view('system_error');
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}
}
?>
